package org.sbbs.base.util.excel;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Layouter {
    private static Logger logger = LoggerFactory.getLogger( Layouter.class );

    /**
     * Builds the report layout.
     * <p>
     * This doesn't have any data yet. This is your template.
     */
    public static void buildReport( Sheet worksheet, int startRowIndex, int startColIndex, String[] labels, String reprotTitle ) {
        // Set column widths

        for ( int i = 0; i < labels.length; i++ ) {

            worksheet.setColumnWidth( i, 5000 );
        }

        /*
         * worksheet.setColumnWidth( 1, 5000 ); worksheet.setColumnWidth( 2, 5000 ); worksheet.setColumnWidth( 3, 5000
         * ); worksheet.setColumnWidth( 4, 5000 ); worksheet.setColumnWidth( 5, 5000 );
         */

        // Build the title and date headers
        buildTitle( worksheet, startRowIndex, startColIndex, reprotTitle );
        // Build the column headers
        buildHeaders( worksheet, startRowIndex, startColIndex, labels );
    }

    /**
     * Builds the report title and the date header
     *
     * @param worksheet
     * @param startRowIndex starting row offset
     * @param startColIndex starting column offset
     */
    public static void buildTitle( Sheet worksheet, int startRowIndex, int startColIndex, String reprotTitle ) {
        // Create font style for the report title
        Font fontTitle = worksheet.getWorkbook().createFont();
        fontTitle.setBoldweight( Font.BOLDWEIGHT_BOLD );
        fontTitle.setFontHeight( (short) 280 );

        // Create cell style for the report title
        CellStyle cellStyleTitle = worksheet.getWorkbook().createCellStyle();
        cellStyleTitle.setAlignment( CellStyle.ALIGN_CENTER );
        cellStyleTitle.setWrapText( true );
        cellStyleTitle.setFont( fontTitle );

        // Create report title
        Row rowTitle = worksheet.createRow( (short) startRowIndex );
        rowTitle.setHeight( (short) 500 );
        Cell cellTitle = rowTitle.createCell( startColIndex );
        cellTitle.setCellValue( reprotTitle );
        cellTitle.setCellStyle( cellStyleTitle );

        // Create merged region for the report title
        worksheet.addMergedRegion( new CellRangeAddress( 0, 0, 0, 5 ) );

        // Create date header
        Row dateTitle = worksheet.createRow( (short) startRowIndex + 1 );
        Cell cellDate = dateTitle.createCell( startColIndex );

        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" );


        cellDate.setCellValue( "报告生成时间： " +sdf.format(  new Date()) );
    }

    /**
     * Builds the column headers
     *
     * @param worksheet
     * @param startRowIndex starting row offset
     * @param startColIndex starting column offset
     */
    public static void buildHeaders( Sheet worksheet, int startRowIndex, int startColIndex, String[] titles ) {
        // Create font style for the headers
        Font font = worksheet.getWorkbook().createFont();
        font.setBoldweight( Font.BOLDWEIGHT_BOLD );

        // Create cell style for the headers
        CellStyle headerCellStyle = worksheet.getWorkbook().createCellStyle();
        headerCellStyle.setFillBackgroundColor( HSSFColor.GREY_25_PERCENT.index );
        headerCellStyle.setFillPattern( CellStyle.LESS_DOTS );
        headerCellStyle.setAlignment( CellStyle.ALIGN_CENTER );
        headerCellStyle.setVerticalAlignment( CellStyle.VERTICAL_CENTER );
        headerCellStyle.setWrapText( true );
        headerCellStyle.setFont( font );
        headerCellStyle.setBorderBottom( CellStyle.BORDER_THIN );

        /*
         * private Long demoId; private int intField; private long longField; private short shortField; private float
         * floatField; private double doubleField;
         */

        // Create the column headers
        Row rowHeader = worksheet.createRow( (short) startRowIndex + 2 );
        rowHeader.setHeight( (short) 500 );
        for ( int i = 0; i < titles.length; i++ ) {

            Cell cell1 = rowHeader.createCell( startColIndex + i );
            cell1.setCellValue( titles[i] );
            cell1.setCellStyle( headerCellStyle );
        }

        
        
        
        
        
        /*
         * Cell cell2 = rowHeader.createCell( startColIndex + 1 ); cell2.setCellValue( "intField" ); cell2.setCellStyle(
         * headerCellStyle ); Cell cell3 = rowHeader.createCell( startColIndex + 2 ); cell3.setCellValue( "longField" );
         * cell3.setCellStyle( headerCellStyle ); Cell cell4 = rowHeader.createCell( startColIndex + 3 );
         * cell4.setCellValue( "shortField" ); cell4.setCellStyle( headerCellStyle ); Cell cell5 = rowHeader.createCell(
         * startColIndex + 4 ); cell5.setCellValue( "floatField" ); cell5.setCellStyle( headerCellStyle ); Cell cell6 =
         * rowHeader.createCell( startColIndex + 5 ); cell6.setCellValue( "doubleField" ); cell6.setCellStyle(
         * headerCellStyle );
         */
    }
}